﻿@{
    Layout = null;
    MICZ.CodeBuilder.ViewModels.WorkContent WorkContent = ViewBag.WorkContent;
}
@using MICZ.CodeBuilder.IRepository.SqlSugar;
@inject ISqlSugarRepository SqlSugarRepository;
<script src="@WorkContent.ThemePath/js/jquery-3.4.1.min.js"></script>
<link href="@WorkContent.ThemePath/layui/css/layui.css" rel="stylesheet" />
<script src="@WorkContent.ThemePath/layui/layui.js"></script>
<script src="@WorkContent.ThemePath/bais.js"></script>

<blockquote class="layui-elem-quote">@ViewData["title"]</blockquote>

<blockquote class="layui-elem-quote">
    <div class="layui-inline">
        <span>命名空间:</span>
        <div class="layui-input-inline">
            <input id="namespace" type="text" value="" placeholder="请输入命名空间" class="layui-input" />
        </div>
    </div>
    <div class="layui-inline">
        <button id="interface" type="button" class="layui-btn layui-inline" onmouseover="Tips(1)">一键生成项目</button>
        <button id="achieve" type="button" class="layui-btn layui-btn-warm layui-inline" onmouseover="Tips(2)">一键生成项目</button>
    </div>
</blockquote>
<script>
    if (localStorage.getItem("namespace") != null)
        $("#namespace").val(localStorage.getItem("namespace"));
    else {
        $.ajax({
            type: "GET",
            url: "/ManageAPI/Builder/Namespace",
            //data: { "ID": (obj.data).ID },
            dataType: "json",
            headers: header,
            success: function (data) {
                if (data.Status) {
                    $("#namespace").val(data.ResData)
                    localStorage.setItem("namespace", $("#namespace").val());
                }
            },
            error: function (data) {
            }
        });
    }
    (function () {
        $("#namespace").on("change", function () {
            localStorage.setItem("namespace", $("#namespace").val());
            //代码获取
            $.ajax({
                type: "GET",
                url: "/ManageAPI/Builder/NamespaceEdit?text=" + $("#namespace").val(),
                //data: { "ID": (obj.data).ID },
                dataType: "json",
                headers: header,
                success: function (data) {
                },
                error: function (data) {
                }
            });
        });
        $("#interface").on("click", function () {
            //代码获取
            $.ajax({
                type: "GET",
                url: "/ManageAPI/Builder/Project?Type=IApplocation&text=" + $("#namespace").val(),
                //data: { "ID": (obj.data).ID },
                dataType: "json",
                headers: header,
                success: function (data) {
                    layui.layer(data.Msg);
                },
                error: function (data) {
                    layui.layer(data);
                }
            });
        });
        $("#achieve").on("click", function () {
            //代码获取
            $.ajax({
                type: "GET",
                url: "/ManageAPI/Builder/Project?Type=Applocation&text=" + $("#namespace").val(),
                //data: { "ID": (obj.data).ID },
                dataType: "json",
                headers: header,
                success: function (data) {
                    layui.layer(data.Msg);
                },
                error: function (data) {
                    layui.layer(data);
                }
            });
        });
    })();
</script>

<div class="layui-row">
    <div class="layui-col-xs12 layui-col-sm3 layui-col-md2">
        <div class="layui-side-scroll">
            <h2>选择表生成代码</h2>
            <ul class="layui-nav layui-nav-tree site-demo-table-nav">
                <hr />
                @{
                    int i = 0;
                    foreach (var item in SqlSugarRepository.Db.DbMaintenance.GetTableInfoList())
                    {
                        //dynamic _dynamic = new
                        //{
                        //    TableName = item.Name,
                        //    Description = item.Description,
                        //    Type = item.DbObjectType,
                        //};
                        <li class="layui-nav-item">
                            <a href="#" data-Name="@item.Name" data-Description="@item.Description" id="Table_@i" onclick="javascript: TableName = this.text; Description =this.getAttribute('data-Description'); OnTableColumns();">@item.Name</a>
                        </li>
                        i++;
                    }
                }
                <hr>
                <span class="layui-nav-bar" style="top: 226px; height: 0px; opacity: 0;"></span>
            </ul>
        </div>
    </div>
    <div class="layui-col-xs12 layui-col-sm9 layui-col-md10" style="height:100%;">
        <div class="layui-tab layui-tab-brief" lay-filter="demoTitle">
            <ul class="layui-tab-title site-demo-title">
                <li class="layui-this">表结构</li>
                <li class="">IApplocation代码</li>
                <li class="">Applocation代码</li>
            </ul>
            <div class="layui-body layui-tab-content site-demo site-demo-body" style="left: 0px;top: 60px;right: unset;bottom: unset;">
                <div class="layui-tab-item layui-show">
                    <div class="layui-main">
                        <table class="layui-hide" id="test" lay-filter="test"></table>

                        <script type="text/html" id="toolbarDemo">
                            <div class="layui-btn-container">
                                <button class="layui-btn layui-btn-sm" lay-event="Create">生成</button>
                            </div>
                        </script>
                        @*<script type="text/html" id="barDemo">
                                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                            </script>*@
                    </div>
                </div>
                <!-- 第二个选项卡 -->
                <div class="layui-tab-item">
                    @*代码预览*@
                    @*<button type="button" class="layui-btn layui-btn-xs layui-btn-normal">保存代码</button>
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-warm">下载代码</button>*@
                    @*<button type="button" class="layui-btn layui-btn-danger">警告按钮</button>*@
                    <pre class="layui-code" id="layui-code-IApplocation">      </pre>
                </div>
                <!-- 第三个选项卡 -->
                <div class="layui-tab-item">
                    @*代码预览*@
                    @*<button type="button" class="layui-btn layui-btn-xs layui-btn-normal">保存代码</button>
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-warm">下载代码</button>*@
                    @*<button type="button" class="layui-btn layui-btn-danger">警告按钮</button>*@
                    <pre class="layui-code" id="layui-code-Applocation">      </pre>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var TableName = $("#Table_0").text();
    var Description = "";
    var table = null;
    //console.log("jiazai");
    layui.use(['table', 'code', 'element'], function () {
        table = layui.table;

        OnTableColumns();

        layui.code();
        var element = layui.element;
        //触发事件
        element.on('tab(demoTitle)', function (elem) {
            location.hash = 'test=' + $(this).attr('lay-id');
        });
    });
    function Tips(Type) {//小提示
        if (Type != 1 && Type != 2)
            return;
        let id = "#interface";
        let tip = "接口层";
        //小tips
        if (Type == 2) {
            id = "#achieve";
            tip = "实现层";
        }
        layer.tips(tip, id, {
            tips: [1, '#3595CC'],
            time: 1000
        });
    }

    function OnTableColumns() {
        table.render({
            elem: '#test'
            , url: '/ManageAPI/Home/TableColumnsList?TableName=' + TableName
            , toolbar: '#toolbarDemo'
            , method: 'POST'
            , title: '表结构信息'
            , headers: header
            //, where: tableData
            , cols: [[
                { type: 'checkbox', fixed: 'left' }
                , { field: 'ColumnsName', title: '列名', fixed: 'left', width: 120, unresize: false, sort: true }
                , {
                    field: 'IsPrimarykey', title: '是否为主键', align: "left", width: 100, sort: false,
                    templet: function (d) { if (d.IsPrimarykey) return '<a class="layui-btn layui-btn-xs">' + d.IsPrimarykey + '</a>'; else return '<a class="layui-btn layui-btn-danger layui-btn-xs">' + d.IsPrimarykey + '</a>'; }
                }
                , {
                    field: 'IsIdentity', title: '是否自增', align: "left", width: 100, sort: false,
                    templet: function (d) { if (d.IsIdentity) return '<a class="layui-btn layui-btn-xs">' + d.IsIdentity + '</a>'; else return '<a class="layui-btn layui-btn-danger layui-btn-xs">' + d.IsIdentity + '</a>'; }
                }
                , {
                    field: 'IsNullable', title: '是否可以为Null', align: "left", width: 100, sort: false,
                    templet: function (d) { if (d.IsNullable) return '<a class="layui-btn layui-btn-xs">' + d.IsNullable + '</a>'; else return '<a class="layui-btn layui-btn-danger layui-btn-xs">' + d.IsNullable + '</a>'; }
                }
                , { field: 'DefaultValue', title: '默认值', width: 120 }
                , { field: 'Length', title: '长度', width: 120 }
                , { field: 'DataType', title: '类型', width: 120 }
                , { field: 'Description', title: '备注', width: 120 }
                //, {
                //    field: 'ISANSWER', title: '是否是正确答案', width: 150, templet: function (res) {
                //        return (res.ISANSWER == 1 ? '<em>是</em>' : '<em>否</em>')
                //    }
                //}
                //, { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 160 }
            ]], parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.Status, //解析接口状态
                    "msg": res.Msg, //解析提示文本
                    "count": res.TotalCount, //解析数据长度
                    "data": res.Data.Obj //解析数据列表
                };
            }
            , response: {
                statusCode: 1 //规定成功的状态码，默认：0
            }
            , page: true
        });
        //代码获取
        $.ajax({
            type: "GET",
            url: "/ManageAPI/Builder/Index?Type=IApplocation&TableName=" + TableName + "&Description=",
            //data: { "ID": (obj.data).ID },
            dataType: "json",
            headers: header,
            success: function (data) {
                if (data.Status == 1) {
                    $("#layui-code-IApplocation").html(data.Data.Obj);
                    layui.code({
                        elem: '#layui-code-IApplocation' //默认值为.layui-code
                        , title: 'BuliderCode'
                    });
                }
            },
            error: function (data) {
                layer.alert("提交出现了错误！");
            }
        });
        $.ajax({
            type: "GET",
            url: "/ManageAPI/Builder/Index?Type=Applocation&TableName=" + TableName + "&Description=",
            //data: { "ID": (obj.data).ID },
            dataType: "json",
            headers: header,
            success: function (data) {
                if (data.Status == 1) {
                    $("#layui-code-Applocation").html(data.Data.Obj);
                    layui.code({
                        elem: '#layui-code-Applocation' //默认值为.layui-code
                        , title: 'BuliderCode'
                    });
                }
            },
            error: function (data) {
                layer.alert("提交出现了错误！");
            }
        });
    };
</script>
